package com.free.ext.baidu;

import com.baidu.aip.ocr.AipOcr;
import com.free.exception.SystemRuntimeException;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
public class BaiduSdkOcr {
    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/

    private String appId;

    private String appKey;

    private String secretKey;

    private int connectionTimeoutInMillis = 2000;

    private int socketTimeoutInMillis= 60000;

    private Proxy httpProxy;

    private Proxy socketProxy;

    private HashMap<String,String> defaultOptions = new HashMap<>();



    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    public BaiduSdkOcr(){
        defaultOptions.put("language_type", "CHN_ENG");
        defaultOptions.put("detect_direction", "true");
        defaultOptions.put("detect_language", "true");
        defaultOptions.put("accuracy", "normal");
        defaultOptions.put("probability", "true");
    }

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    public JSONObject analyze(byte[] content){
        return this.getClient().basicGeneral(content, this.defaultOptions);
    }

    public JSONObject vehicleLicense(byte[] content){
        return this.getClient().vehicleLicense(content, this.defaultOptions);
    }


    public AipOcr getClient(){
        isValid();
        AipOcr ocr = new AipOcr(appId,appKey,secretKey);

        if(httpProxy != null){
            ocr.setHttpProxy(httpProxy.getHost(),httpProxy.getPort());
        }
        if(socketProxy != null){
            ocr.setSocketProxy(socketProxy.getHost(), socketProxy.getPort());
        }
        ocr.setConnectionTimeoutInMillis(connectionTimeoutInMillis);
        ocr.setSocketTimeoutInMillis(socketTimeoutInMillis);
        return ocr;
    }

    public void setOptions(Map<String,String> options){
        this.defaultOptions = (HashMap<String, String>) options;
    }

    private void isValid(){
        if(StringUtils.isEmpty(appId)
                || StringUtils.isEmpty(appKey)
                || StringUtils.isEmpty(secretKey)){
            throw new SystemRuntimeException("BaiduApi 信息导入失败");
        }
        if(httpProxy != null && socketProxy != null){
            throw new SystemRuntimeException("BaiduApi httpProxy与SocketProxy 重复");
        }
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppKey() {
        return appKey;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public Integer getConnectionTimeoutInMillis() {
        return connectionTimeoutInMillis;
    }

    public void setConnectionTimeoutInMillis(Integer connectionTimeoutInMillis) {
        this.connectionTimeoutInMillis = connectionTimeoutInMillis;
    }

    public Integer getSocketTimeoutInMillis() {
        return socketTimeoutInMillis;
    }

    public void setSocketTimeoutInMillis(Integer socketTimeoutInMillis) {
        this.socketTimeoutInMillis = socketTimeoutInMillis;
    }

    public Proxy getHttpProxy() {
        return httpProxy;
    }

    public void setHttpProxy(Proxy httpProxy) {
        this.httpProxy = httpProxy;
    }

    public Proxy getSocketProxy() {
        return socketProxy;
    }

    public void setSocketProxy(Proxy socketProxy) {
        this.socketProxy = socketProxy;
    }

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
